# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def flatten(self, root):
        """
        :type root: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
        path = []
        def traverse(root):
            if not root: return
            path.append(root)
            traverse(root.left)
            traverse(root.right)
        traverse(root)
        for i, r in enumerate(path):
            r.left = None
            r.right = path[i+1] if i+1<len(path) else None